<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
//use think\view;
use app\admin\model\IndexModel;
use think\facade\Db;
use think\facade\View;
class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->checkLogin();
        View::assign('left', 'index');
    }

    public function index()
    {
        $oModel = new IndexModel();
        $where = [];
        if ($name = input('get.name'))
            $where[] = ["dyname",'like',"%{$name}%"];
        $list = $oModel->select($where);
        return view('./index/index',[
            'list'=>$list,
            'name' => isset($name) ? $name : '',
        ]);
    }
    public function edit()
    {
        if($id=input('get.')){
            $oModel=new IndexModel();
            $list= $oModel->find($id);
        }else{
            $post=input('post.');
            if(!empty($post['pwd'])){
                $post['pwd'] = md5($post['pwd']);
            }
            $edit= new IndexModel();
            $edit1=$edit->edit($post);
            if($edit1){
                $this->success('修改成功','/admin.php/Index/index');
            }else{
                $this->error('修改失败','/admin.php/Index/edit');
            }
        }
        return view('./index/edit',[
            'list'=>$list,
        ]);
    }
    public function del(){
        $id=$_GET['id'];
        $oModel=new IndexModel();
        $del=$oModel->del($id);
        return view();
    }
    public function edit_stutic(){
        if(request()->isPost()){
            $post=input('post.');
            $list=Db::name('daoyou')->where('id',$post['id'])->find();
            if($list['stutic'] == 1){
                $stutic = 2;
                Db::name('daoyou')->where('id',$post['id'])->update([
                    'stutic' => $stutic
                ]);
            }else{
                $stutic = 1;
                Db::name('daoyou')->where('id',$post['id'])->update([
                    'stutic' => $stutic
                ]);
            }
            return ['code' => 1];
        }
    }
}
